package com.semidata.rtd.core.http.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

public abstract class AbstractDispatchHandler implements HttpDispatchHandler {

	private ChannelHandlerContext ctx;
	private HttpRequest request;
	private ByteBuf buffer = Unpooled.buffer();

	public AbstractDispatchHandler(ChannelHandlerContext ctx, HttpRequest request) {
		this.ctx = ctx;
		this.request = request;
	}
	
	@Override
	public void handleData(ByteBuf data) {
		buffer.writeBytes(data);
	}

	@Override
	public void complete() {
		HttpSession session = new HttpSession(ctx, request, buffer);
		handle(session);
	}

	@Override
	public void exceptionCaught(Throwable cause) {
	}
	
	public abstract void handle(HttpSession session);

}
